package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.MarketIssueService;
import com.cskaoyan.market.service.impl.MarketIssueServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/issue/*")
public class IssueController extends HttpServlet {
    private final MarketIssueService marketIssueService = new MarketIssueServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/issue/", "");
        System.out.println("dispatcher: GET " + op);
        if ("list".equals(op)){
            list(req,resp);
        }
    }

    /**
     * 1.从service层得到list
     * 2.处理返回值
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer limit = Integer.parseInt(req.getParameter("limit"));
        String question = req.getParameter("question");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketIssue> marketIssues = marketIssueService.readIssueList(page, limit, question, sort, order);
        Object ok = ResponseUtil.okList(marketIssues);
        //哪个方法可以设置响应体？？
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/issue/", "");
        System.out.println("dispatcher: POST " + op);
        if ("create".equals(op)){
            create(req,resp);
        }else if ("update".equals(op)){
            update(req,resp);
        }else if ("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);

        String question = JacksonUtil.parseString(jsonStr, "question");
        String answer = JacksonUtil.parseString(jsonStr, "answer");
        List<MarketIssue> marketIssues = marketIssueService.InsertIssue(question, answer);
        if (marketIssues.size()>0){
            MarketIssue marketIssue = marketIssues.get(0);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id",marketIssue.getId());
            dataMap.put("question",marketIssue.getQuestion());
            dataMap.put("answer",marketIssue.getAnswer());
            dataMap.put("addTime",marketIssue.getAddTime());
            dataMap.put("updateTime",marketIssue.getUpdateTime());
            Object ok = ResponseUtil.ok(dataMap);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }

    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);
        String id = JacksonUtil.parseString(jsonStr, "id");
        String question = JacksonUtil.parseString(jsonStr, "question");
        String answer = JacksonUtil.parseString(jsonStr, "answer");
        String updateTime = JacksonUtil.parseString(jsonStr, "updateTime");
        String addTime = JacksonUtil.parseString(jsonStr, "addTime");
        List<MarketIssue> marketIssues = marketIssueService.updateIssue(id, question, answer,addTime,updateTime);
        if (marketIssues.size()>0){
            Object ok = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);
        String id = JacksonUtil.parseString(jsonStr, "id");
        String question = JacksonUtil.parseString(jsonStr, "question");
        String answer = JacksonUtil.parseString(jsonStr, "answer");
        String updateTime = JacksonUtil.parseString(jsonStr, "updateTime");
        String addTime = JacksonUtil.parseString(jsonStr, "addTime");
        List<MarketIssue> marketIssues = marketIssueService.deleteIssue(id, question, answer,addTime,updateTime);
        if (marketIssues.size()>0){
            Object ok = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }
}
